package com.ttbj.websocket.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.ttbj.controller.enums.UserTypeEnum;
import com.ttbj.websocket.handler.AppletWebSocketHandler;
import com.ttbj.websocket.interceptor.AndoridWebSocketHandlerInterceptor;
import com.ttbj.websocket.interceptor.AppletWebSocketHandlerInterceptor;

@Configuration
@EnableWebMvc
@EnableWebSocket
public class  DefultWebSocketConfig extends  WebMvcConfigurerAdapter implements WebSocketConfigurer {
	
	public static final Logger logger = LoggerFactory.getLogger(DefultWebSocketConfig.class);
	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		// 小程序server实现类，小程序用户访问websocket的地址/applet.do
		registry.addHandler(appletHandle(), "/applet.do").addInterceptors(appletWebSocketInterceptor());
		// android server实现类android用户访问websocket的地址/android.do
		registry.addHandler(androidHandle(), "/android.do").addInterceptors(andoridWebSocketInterceptor());
	}

	/**
	 * 小程序
	 * 
	 * @return 消息发送的Bean
	 */
	@Bean(name = "appletWebSocketHandler")
	public WebSocketHandler appletHandle() {
		return new AppletWebSocketHandler(UserTypeEnum.APPLET_TYPE.getType());
	}

	/**
	 * 安卓
	 * 
	 * @return 消息发送的Bean
	 */
	@Bean(name = "androidWebSocketHandler")
	public WebSocketHandler androidHandle() {
		return new AppletWebSocketHandler(UserTypeEnum.ANDROID_TYPE.getType());
	}

	/**
	 * 小程序拦截器
	 */
	@Bean(name = "appletWebSocketInterceptor")
	public AppletWebSocketHandlerInterceptor appletWebSocketInterceptor() {
		return new AppletWebSocketHandlerInterceptor(UserTypeEnum.APPLET_TYPE.getType());
	}

	/**
	 * 安卓拦截器
	 */
	@Bean(name = "andoridWebSocketInterceptor")
	public AndoridWebSocketHandlerInterceptor andoridWebSocketInterceptor() {
		return new AndoridWebSocketHandlerInterceptor(UserTypeEnum.ANDROID_TYPE.getType());
	}

}
